package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.Admin;
import com.cskaoyan.bean.admin.bo.PasswordBo;
import com.cskaoyan.service.admin.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-21
 */
@RestController
public class ProfileController {

    @Autowired
    AdminService adminService;

    @PostMapping("admin/profile/password")
    // http://localhost:8083/admin/profile/password
    public BaseRespVo password(@RequestBody @Validated PasswordBo passwordBo, BindingResult bindingResult) {
        if (!passwordBo.getNewPassword().equals(passwordBo.getNewPassword2()))
            return BaseRespVo.error("两次输入的密码不一致");
        Admin principal = (Admin) SecurityUtils
                .getSubject()
                .getPrincipals()
                .getPrimaryPrincipal();
        boolean result = adminService.updatePasswordByUsername(principal, passwordBo);
        if (!result) return BaseRespVo.error("修改失败");
        return BaseRespVo.ok();
    }
}
